package com.itany.controller;


import com.github.pagehelper.PageInfo;
import com.itany.constant.StatusConst;
import com.itany.exception.ManagerServiceException;
import com.itany.pojo.BookWithBLOBs;
import com.itany.pojo.Examine;
import com.itany.pojo.ManagerUser;
import com.itany.service.BookService;
import com.itany.service.ExamineService;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpSession;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/examine")
public class ExamineController {

    @Autowired
    ExamineService service;

    @Autowired
    BookService bookService;

    @RequestMapping("/add")
    public ActionResult add(
            BookWithBLOBs book,
            @RequestParam(value = "img", required = false) CommonsMultipartFile file,
            HttpSession session) {
        ActionResult result = new ActionResult();
        try {
            ManagerUser managerUser = (ManagerUser) session.getAttribute(StatusConst.LOGIN_USER);
            if (managerUser == null) {
                throw new Exception("没登陆呢");
            }
            service.addExamine(managerUser.getSupplierid(), book, file);
        } catch (ManagerServiceException e) {
            result.setStatus(StatusConst.FAIL_RESPONSE);
            result.setMsg(e.getMessage());
        } catch (Exception e) {
            result.setStatus(StatusConst.FAIL_RESPONSE);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @RequestMapping("/findAll")
    @ResponseBody
    public Map<String, Object> findAll(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                       @RequestParam(name = "rows", defaultValue = "5") Integer rows,
                                       String bookname,
                                       String isbn,
                                       Integer supplierId
    ) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            PageInfo<Examine> info = service.findBooksByExample(page, rows, bookname, isbn, supplierId);
            map.put("total", info.getTotal());
            map.put("rows", info.getList());
        } catch (ManagerServiceException e) {
            map.put("msg", e.getMessage());
        }
        return map;
    }

    @RequestMapping("/findOne")
    public ActionResult findOne(Integer id) {
        ActionResult result = new ActionResult();
        try {
            Examine ex = service.findById(id);
            result.setData(ex);
        } catch (Exception e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @RequestMapping("/update")
    public ActionResult update(Integer id,
                               Integer flag,
                               String info) {
        ActionResult result = new ActionResult();
        try {
            Examine ex = service.update(id, flag, info);
            result.setData(ex);
        } catch (Exception e) {
            result.setMsg(e.getMessage());
        }
        return result;
    }




    @RequestMapping("/supplierFindAll")
    @ResponseBody
    public Map<String, Object> supplierFindAll(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                               @RequestParam(name = "rows", defaultValue = "5") Integer rows,
                                               String bookname,
                                               String isbn,
                                               HttpSession session
    ) {

        ManagerUser managerUser = (ManagerUser) session.getAttribute(StatusConst.LOGIN_USER);
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            if (managerUser == null) {
                throw new Exception("没登陆呢");
            }
            PageInfo<Examine> info = service.findBooksByExample(page, rows, bookname, isbn, managerUser.getSupplierid());
            map.put("total", info.getTotal());
            map.put("rows", info.getList());
        } catch (Exception e) {
            map.put("msg", e.getMessage());
        }
        return map;
    }


    /**
     * C:\Users\L\AppData\Local\Temp\tomcat-docbase.4432130332034197647.9001/upload\20210831
     * /C:/projects3/test-parent/test-manager/manager-web/target/classes/
     *
     * @param session
     * @return
     * @throws FileNotFoundException
     */
    @RequestMapping("/abspath")
    public ActionResult showPath(HttpSession session) throws FileNotFoundException {
        ActionResult result = new ActionResult();
        String path = session.getServletContext().getRealPath("/upload/" + new SimpleDateFormat("yyyyMMdd").format(new Date()));
        //
        System.out.println(path);
        String absPath = ResourceUtils.getURL("classpath:").getPath();
        //
        System.out.println(absPath);
        return result;
    }
}
